package com.duoduo.深度广度优先;

import com.duoduo.TreeNode;
/**
 * @program: algorithm
 * @description: 二叉树的最大深度104
 * 给定一个二叉树，找出其最大深度。
 * 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
 * @author: chaidl  ！
 * @create: 2022-03-01 14:22
 */
public class 二叉树的最大深度 {
    public static void main(String[] args) {
        TreeNode A = new TreeNode(3);
        TreeNode c = new TreeNode(9);
        TreeNode v = new TreeNode(20);
        TreeNode l = new TreeNode(15);
        TreeNode j = new TreeNode(7);
        A.left = c;
        A.right = v;
        v.left=l;
        v.right=j;
        System.out.println(maxDepth(A));
    }
    public int maxDepth1(TreeNode root) {
        if(root==null){
            return 0;
        }else{
            return Math.max(maxDepth(root.left),maxDepth(root.right))+1;
        }
    }
    public static int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftHeight = maxDepth(root.left);
        int rightHeight = maxDepth(root.right);
        return Math.max(leftHeight, rightHeight) + 1;
    }
}
